package com.zsj.service_user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.zsj.common_util.R;
import com.zsj.service_user.entity.AppGoods;
import com.zsj.service_user.entity.AppUser;
import com.zsj.service_user.service.AppGoodsService;
import com.zsj.service_user.util.TokenMd5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zsj
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/serviceUser/app_goods")
@CrossOrigin
@Slf4j
public class AppGoodsController {
    @Autowired
    private AppGoodsService appGoodsService;

    /**
     * 获取所有商品信息
     */
    @GetMapping("/getGoodsList")
    public R getGoodsList() {
        List<AppGoods> list = appGoodsService.list(new QueryWrapper<>());
        if (list.isEmpty()) {
            return R.error().message("查询错误,商品列表为空");
        } else
            return R.ok().data("data", list);
    }

    /**
     * 添加商品
     */
    @PostMapping("/addGoods")
    public R addGoods(@RequestParam("token") String token, @RequestBody AppGoods appGoods) {
        //对数据进行数据库的插入
        if (TokenMd5.getMD5(appGoods.getGoodsBelongToUser()).equals(token)) {
            if (appGoodsService.saveGoods(appGoods)) {
                return R.ok().data("data", appGoods);
            } else return R.error().message("添加商品失败");
        } else return R.error().message("身份验证错误");
    }


    /**
     * 根据类型查询商品列表
     */
    @GetMapping("/getGoodsByType")
    public R getGoodsByType(@RequestParam("type") String type) {
        QueryWrapper<AppGoods> queryWrapper = new QueryWrapper();
        queryWrapper.eq("goods_type",type);
        List list = appGoodsService.list(queryWrapper);
        if (list.isEmpty()){
            log.error("错误！在getGoodsByType方法中,你查询的类型商品的数量为0");
            return R.error().message("该类型的商品暂时未上架");
        }
        return R.ok().message("成功").data("list",list);
    }


    /**
     * 根据名称查询商品列表
     */
    @GetMapping("/getGoodsByName")
    public R getGoodsByName(@RequestParam("name") String name) {
        QueryWrapper<AppGoods> queryWrapper = new QueryWrapper();
        queryWrapper.like("goods_name",name);
        List list = appGoodsService.list(queryWrapper);
        if (list.isEmpty()){
            log.error("错误！在getGoodsByType方法中,你查询的类型商品的数量为0");
            return R.error().message("没有名称类似为"+name +"的商品哦");
        }
        return R.ok().message("成功").data("list",list);
    }

    /**
     * 根据id来获取商品信息
     */
    @GetMapping("/getGoodsById")
    public R getGoodsById(@RequestParam("id")Integer id){
        AppGoods goods = appGoodsService.getById(id);
        if (goods == null){
            log.error("错误getGoodsById方法中,根据{}获取的商品不存在"+id);
            return R.error().message("错误！");
        }else {
            log.info("成功获取id为{}的商品"+id);
            return R.ok().message("成功获取").data("info",goods);
        }
    }



}

